

function formatTime(date) {

	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('-')
}
function formatTimeDate(date) {

	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('-') + ' ' + formatNumber(hour) + ':' + formatNumber(minute) + ':' + formatNumber(second)
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

function showLoading(msg) {
	wx.showToast({
		title: msg ? msg : '加载中',
		icon: 'loading',
		mask: true,
		duration: 100000000
	});
}

function hideLoading() {
	wx.hideToast();
}

function showLoading_gif(that) {
	that.setData({
		hidden: false,
	});
}

function hideLoading_gif(that) {
	that.setData({
		hidden: true,
	});
}

function showSuccess(msg, icon, mask, time) {
	icon = icon ? icon.toString() : 'success';
	mask = mask === false || mask === true ? mask : true;
	wx.showToast({
		title: msg ? msg : '成功',
		icon: icon,
		mask: mask,
		duration: time ? time : 2000,
	});
}

function message(msg, success) {
	wx.showModal({
		title: '温馨提示',
		content: msg,
		showCancel: false,
		confirmColor: "#59A5F0",
		success: success
	})
}

function confirm(msg, success, fail) {
	wx.showModal({
		title: '温馨提示',
		content: msg,
		showCancel: true,
		confirmColor: "#59A5F0",
		cancelColor: "#99a0a2",
		success: success,
		fail: fail
	})
}

// get请求方法
function fetchGet(url, callback) {
	// return callback(null, top250)
	wx.request({
		url: getApp().globalData.http_url + url,
		header: {
			'Content-Type': 'application/json'
		},
		success(res) {
			callback(res.data, res.data)
		},
		fail(e) {
			console.error(e)
			callback(e)
		}
	})
}

// post请求方法
function fetchPost(url, data, callback) {
	data.store_id = wx.getStorageSync('store_id') !== undefined ? wx.getStorageSync('store_id') : '1';
	wx.request({
		method: 'POST',
		url: getApp().globalData.http_url + url + '.html',
		data: this.json2Form(data),
		header: {
			"Content-Type": "application/x-www-form-urlencoded",
		},
		success(res) {
			callback(res.data, res.data)
		},
		fail(e) {
			console.error(e)
			callback(e)
		},
		complete: function () {
			// complete
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}
	})
}

function json2Form(json) {
	var str = [];
	for (var p in json) {
		str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
	}
	return str.join("&");
}
function makeTimeStatus(timetext, typeq = '0') {
	if (typeq == 1) {
		var msg_date = new Date();
		msg_date.setTime(timetext * 1000);
		var time3 = timetext;
	} else if (timetext == null || timetext == '' || timetext == undefined) {
		var msg_date = new Date();
		var time3 = timetext;
	} else {
		if (timetext.indexOf('2018') === -1) {
			timetext = '2018/' + timetext;
		}
		var msg_date = new Date(timetext);
		var time3 = Date.parse(msg_date) / 1000;
	}
	var timestamp = Date.parse(new Date()) / 1000;
	if ((timestamp - time3) < 3600) {
		var timestatus = '刚刚';
	} else if ((timestamp - time3) > 3600 && (timestamp - time3) < 86400) {
		var timestatus = msg_date.getHours() + ':' + (msg_date.getMinutes() < 10 ? '0' + msg_date.getMinutes() : msg_date.getMinutes());
	} else if ((timestamp - time3) > 86400 && (timestamp - time3) < 172800) {
		var timestatus = '前天';
	} else if ((timestamp - time3) > 172800 && (timestamp - time3) < 604800) {
		var show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
		var timestatus = show_day[msg_date.getDay()];
	} else if ((timestamp - time3) > 604800 && (timestamp - time3) < 31536000) {
		var timestatus = (msg_date.getMonth() + 1) + '-' + (msg_date.getDate() < 10 ? '0' + msg_date.getDate() : msg_date.getDate());
	} else if ((timestamp - time3) > 31536000) {
		var timestatus = msg_date.getFullYear() + '/' + (msg_date.getMonth() + 1) + '/' + msg_date.getDay();
	} else {
		var timestatus = timetext;
	}
	return timestatus;
}
function getRoute() {
	var pages = getCurrentPages();
	console.log(pages);
	return pages[pages.length - 1].route;
}
function getJson(url) {
	var arr = url.split('?')[1].split('&')
	var theRequest = new Object();
	for (var i = 0; i < arr.length; i++) {
		var kye = arr[i].split("=")[0]
		var value = arr[i].split("=")[1]
		// 给对象赋值
		theRequest[kye] = value
	}
	return theRequest;
}
module.exports = {
	formatTime: formatTime,
	showLoading: showLoading,
	hideLoading: hideLoading,
	showSuccess: showSuccess,
	message: message,
	confirm: confirm,
	fetchGet: fetchGet,
	fetchPost: fetchPost,
	json2Form: json2Form,
	showLoading_gif: showLoading_gif,
	hideLoading_gif: hideLoading_gif,
	getRoute: getRoute,
	makeTimeStatus: makeTimeStatus,
	formatTimeDate: formatTimeDate,
	getJson: getJson
}